# encoding= utf-8
from zipfile import ZipFile
import os
import os.path
import csv
import sys
import glob

from dbfpy import dbf

"""
这个脚本把上交所每天晚上发过来的zip文件给解压，然后生成对应
的csv文件。生成的csv文件由datastage job抽取到数据库dataguard的shangjsgh表里
注意: 这个脚本依赖于外部命令unzip.
"""


def unzip_file_into_dir(file, dir):
    # os.mkdir(dir, 0777)
    zfobj = ZipFile(file)
    fileList = []
    for name in zfobj.namelist():
        if name.endswith('/'):
            os.mkdir(os.path.join(dir, name))
        else:
            outfileName = os.path.join(dir, name)
            fileList.append(outfileName)
            outfile = open(outfileName, 'wb')
            outfile.write(zfobj.read(name))
            outfile.close()
    return fileList

# zips = ZipFile("R:\IMPORT\TRANS\\20071112\gh23675.zip","r")
"""
open the zip file containing the dbf file 
and save it to a csv file with the same name
"""


def dbf_to_csv(filename, dir):
    csvDir = os.path.split(filename)[0]
    fname = os.path.split(filename)[1]
    csvName = csvDir + "/" + os.path.splitext(fname)[0] + ".csv"
    print "csvName=" + csvName
    writer = csv.writer(open(csvName, "wb"))
    db = dbf.Dbf(filename)
    #i = 0
    for rec in db:
        #i = i+1
        record = []
        #print "record " + str(i)
        for fieldName in db.fieldNames:
            #print '%s:	%s' %(fieldName,rec[fieldName])
            record.append(rec[fieldName])
        writer.writerow(record)
    print "total " + str(len(db)) + " record(s)"


if __name__ == '__main__':
    #print sys.argv[1]
    workingDir = sys.argv[1]
    zipFileDir = workingDir + "\gh*.zip"
    #zipFiles = [item for arg in sys.argv for item in glob.glob(arg)]
    zipFiles = [item for item in glob.glob(zipFileDir)]
    #ghFiles = zipFiles[1:]
    #print ghFiles
    for shgh in zipFiles:
        print shgh
        os.system('unzip -o -d ' + workingDir + ' ' + shgh)
    #print "converting to csv"
    for dbfFile in glob.glob(workingDir + "\gh*.dbf"):
        dbf_to_csv(dbfFile, workingDir)
    #zip_to_csv(shgh,workingDir)

